import { Component, OnInit } from '@angular/core';
import { ActivatedRoute, Router } from '@angular/router';
import { InfiniteScrollCustomEvent } from '@ionic/angular';

import * as Parse from "parse"
(Parse as any).serverURL = "http://metapunk.cn:9999/parse"
Parse.initialize("dev")

@Component({
  selector: 'app-home-detail',
  templateUrl: './home-detail.component.html',
  styleUrls: ['./home-detail.component.scss']
})
export class HomeDetailComponent implements OnInit {

  mediumId: string = "";
  userId: string = "";
  medium: any = []
  user: any = {}
  commentUserList: any = []
  constructor(private activeRoute: ActivatedRoute, private router: Router) { }


  async ngOnInit(): Promise<void> {
    this.activeRoute.queryParams.subscribe(params => {
      this.mediumId = params['mediumId'];
      this.userId = params['userId'];
    })
    this.initData(this.mediumId, this.userId)
    console.log("user", this.user)
    console.log("medium", this.medium)
    console.log(typeof (this.user))
    console.log(typeof (this.medium))
    console.log("commentUserList", this.commentUserList)


  }

  async initData(mediumId: string, userId: string) {
    this.medium = this.getMediumById(mediumId)
    this.user = this.getUserById(userId)
    this.commentUserList = this.getcommentUserById(mediumId)
  }

  async getUserById(id: string) {
    let query = new Parse.Query("BRBUser")
    query.equalTo("userId", id)
    this.user = await query.find()
  }
  async getMediumById(id: string) {
    let query = new Parse.Query("BRBMedium")
    query.equalTo("mediumId", id)
    this.medium = await query.find()
  }
  async getcommentUserById(id: string) {
    let query = new Parse.Query("BRBcomment")
    query.equalTo("mediumId", id)
    query.include("user")
    this.commentUserList = await query.find()
  }
  async goPay(item: any) {
    console.log("1" + item);
    // 修改Student
    //     let query2 = new Parse.Query("goods")
    //     let stu1 = await query2.get("GvzX0WaRuR");
    //     stu1.set("goodsID","19")
    //     stu1.save()

    // this.router.navigate(["./intelligent-shopping//pay"],{
    //     queryParams:item
    // })
  }
  userList: any = [];

  async generateUserList() {
    // for(let i=0 ;i<2;i++){
    //   let query = new Parse.Query("BRBUser")
    //   query.limit(30)
    //   let slist:any = await query.find()

    //   console.log(slist)

    //   slist = slist.map((item:any)=>item.toJSON())
    //   this.userList = this.userList.concat(slist);   
    // }
  }



  onIonInfinite(ev: any) {
    this.generateUserList();

    console.log("213")

    setTimeout(() => {
      (ev as InfiniteScrollCustomEvent).target.complete();
    }, 500);
  }
  goBack() {
    this.router.navigate(["/BigRedBook/home"], {
    })
   }

}
